package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class OrderdetailetDaoImpl implements  OrderdetailetDao{
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    /**
     * 向明细表批量添加数据
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public int saveOrderDetailetBath(List<Orderdetailet> list) throws Exception {
        StringBuffer stringBuffer = new StringBuffer("INSERT INTO orderdetailet (orderId,foodId,quantity)VALUES");
        for (Orderdetailet od:list) {
            stringBuffer.append("(\"+od.getOrderId()+\",\"+od.getFoodId()+\",\"+od.getQuantity()+\"),");
        }
        //去掉最后一个逗号
        int result =0;
        String sql = stringBuffer.toString().substring(0,stringBuffer.toString().length()-1);
        try {
            con=  DBUtil.getConnection();
            pst= con.prepareStatement(sql);
            result= pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
    /**
     * 根据订单的Id查询明细列表
     * @param orderId
     * @return
     */
    @Override
    public List<Orderdetailet> listOrderDetailetByOrderId(Integer orderId) {
        List<Orderdetailet>list=new ArrayList<>();
        //sql查询明细与食品之间的关联
        String sql="SELECT d.*,f.* FROM orderdetailet d,food f WHERE d.foodId=f.foodId AND orderId=?";
        try {
            //Connection从ThreadLocal中获取
            con=  DBUtil.getConnection();
            pst= con.prepareStatement(sql);
            pst.setInt(1,orderId);
            //查询
            rs= pst.executeQuery();
            while (rs.next()){
                Orderdetailet detailet=new Orderdetailet();
                detailet.setOrderId(rs.getInt("orderId"));
                detailet.setQuantity(rs.getInt("quantity"));
                detailet.setFoodId(rs.getInt("foodId"));
                detailet.setOdId(rs.getInt("odId"));
                Food food=new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                detailet.setFood(food);
                list.add(detailet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}
